﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Minimalized.GameObjects
{
    class Translocator : GameObject
    {
        #region Fields
        private static Texture2D transTexture = Texture_Library.translocator_Texture;
        private static Point transSize = new Point(24, 8);
        private static Vector2 velocity = new Vector2(3, 3);
        private static Vector2 movement;
        private static Vector2 direction;
        private static Point pos;
        

        private float maxVel = 5;

        private bool isFired = false;

        MouseState mState_current;
        MouseState mState_last;
        #endregion

        public Translocator(Vector2 position)
            : base(transSize, position, movement, transTexture)
        {
            movement = new Vector2(Mouse.GetState().X - rectangle.X, Mouse.GetState().Y - rectangle.Y);
            if (movement != Vector2.Zero)
                movement.Normalize();

            pos = new Point((int)position.X, (int)position.Y);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="playerposition">spelarens position(startposition för translocator)</param>
        /// <param name="gametime">gamtime</param>
        public void Shoot(Vector2 playerposition, Vector2 mouse_currentPos, GameTime gametime)
        {
            direction = new Vector2(mouse_currentPos.X - pos.X, mouse_currentPos.Y - pos.Y);
            pos.X += (int)(direction.X * velocity.X * gametime.ElapsedGameTime.TotalSeconds);
            pos.Y += (int)(direction.Y * velocity.Y * gametime.ElapsedGameTime.TotalSeconds);

        }
        public void Update(Vector2 playerposition, GameTime gameTime)
        {

            rectangle = new Rectangle(pos.X, pos.Y, transSize.X, transSize.Y);
            mState_current = Mouse.GetState();

            if (isFired)
            {

                if (mState_last.LeftButton == ButtonState.Released && mState_current.LeftButton == ButtonState.Pressed)
                {
                    //Resetar positionen
                    pos.X = (int)playerposition.X;
                    pos.Y = (int)playerposition.Y;
                    isFired = false;
                }
            }

            if (mState_last.LeftButton == ButtonState.Released && mState_current.LeftButton == ButtonState.Pressed)
            {
                isFired = true;
                Shoot(new Vector2(playerposition.X, playerposition.Y), new Vector2(mState_current.X, mState_current.Y), gameTime);
            }

            if (isFired)
            {
                pos.X += (int)(direction.X * velocity.X * gameTime.ElapsedGameTime.TotalSeconds);
                pos.Y += (int)(direction.Y * velocity.Y * gameTime.ElapsedGameTime.TotalSeconds);
            }

            mState_last = mState_current;
            base.Update(gameTime);
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            if(isFired == true)
            base.Draw(spriteBatch);
        }

        public Point returnPosition()
        {
            return pos;
        }
    }
}
